package com.xxrl.shop.controller;

import com.xxrl.shop.common.entity.JsonResult;
import com.xxrl.shop.common.utils.PageTool;
import com.xxrl.shop.common.utils.ResultTool;
import com.xxrl.shop.domain.Apply;
import com.xxrl.shop.domain.Merchant;
import com.xxrl.shop.domain.User;
import com.xxrl.shop.service.ApplyService;
import com.xxrl.shop.service.MerchantService;
import com.xxrl.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

/**
 * @author mis
 */
@Api(tags = {"merchant"})
@RestController
@RequestMapping("/merchant")
public class MerchantController {

    private final MerchantService merchantService;
    private final ApplyService applyService;
    private final UserService userService;


    public MerchantController(MerchantService merchantService, ApplyService applyService, UserService userService) {
        this.merchantService = merchantService;
        this.applyService = applyService;
        this.userService = userService;
    }

    @ApiOperation(value = "List merchants by page request.", response = Merchant.class, responseContainer = "List")
    @GetMapping("list")
    public JsonResult<String> list(@ApiParam(value = "Page number", defaultValue = "0", example = "0") @RequestParam(defaultValue = "0", name = "page") Integer page,
                                   @ApiParam(value = "Page size", defaultValue = "0", example = "0") @RequestParam(defaultValue = "10", name = "limit") Integer size) {
        Page<Merchant> merchant = merchantService.findAll(PageRequest.of(page, size));
        return ResultTool.success(PageTool.pageToJson(merchant));
    }

    @ApiOperation(value = "Get merchant info by user", response = Merchant.class)
    @GetMapping("get")
    public Merchant getMerchant(User user) {
        return merchantService.getMerchantByUser(user);
    }

    /**
     * 管理员接收用户成为商家的请求
     *
     * @param userId   用户id
     * @param merchant 商家信息
     * @return 成功响应Json
     */
    @PostMapping("add")
    public JsonResult<String> add(Long userId,
                                  @ApiParam(example = "Merchant") Merchant merchant) {
        // 修改用户的 是否为商家 并 将权限改为商家
        // TODO 这里感觉 isMerchant逻辑重复
        User user = userService.getUserById(userId);
        user.setIsMerchant(true);
        user.setRole("merchant");
        // 增加商家, 保存修改后的用户, 删除申请
        merchantService.addMerchantByUser(user, merchant);
        userService.saveUser(user);
        applyService.delete(user.getId());
        return ResultTool.success("OK");
    }

    @PostMapping("save")
    public void save(@ApiParam(example = "Merchant") Merchant merchant) {
        merchantService.saveMerchant(merchant);
    }


    /**
     * 用户发送成为商家的申请
     * @param userId 用户id
     * @return 申请的结果
     */
    @PostMapping("apply")
    public JsonResult<String> applyMerchant(Long userId) {
        if (!applyService.exists(userId)) {
            applyService.insert(userId);
            return ResultTool.success("申请发送成功");
        } else {
            return ResultTool.success("申请已经存在!");
        }
    }

    @GetMapping("apply/list")
    public JsonResult<String> applyList(@RequestParam(defaultValue = "0", name = "page")Integer page,
                                        @RequestParam(defaultValue = "10", name = "limit")Integer size) {
        Page<Apply> applies = applyService.findAll(PageRequest.of(page, size));
        return ResultTool.success(PageTool.pageToJson(applies));
    }

    @PostMapping("apply/delete")
    public void deleteApply(Long userId) {
        applyService.delete(userId);
    }
}
